import 'package:get/get.dart';
import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/network/xindong_response.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/routes/tab_moments/model/xindong_dongtai_class.dart';
import 'package:xindong/routes/tab_moments/publish/xindong_dongtai_publish_class.dart';
import 'package:xindong/widgets/dialog/xindong_alert_message.dart';

class XinDongDongTaiHandle {
  // 请求加载列表
  //
  // type = 0推荐全部 = 1关注
  // yemian从0开始
  // yemiansize每页数量
  void requestListWithType({
    required int type,
    required int yemian,
    int yemianSize = 10,
    required Function(NetworkResponse) callback,
  }) {
    var parameters = {
      "page": yemian,
      "pagesize": yemianSize,
      "type": type,
      "longitude": 0,
      "latitude": 0,
    };
    XinDongNetworking.instance
        .request(XinDongApi.momentList, method: XinDongHttpMethod.post, parameters: parameters)
        .then(callback);
  }

  // 请求加载列表
  //
  // type = 0推荐全部 = 1关注
  // yemian从0开始
  // yemiansize每页数量
  void requestListWithUserId({
    required String userId,
    required int yemian,
    int yemianSize = 10,
    required Function(NetworkResponse) callback,
  }) {
    var parameters = {
      "page": yemian,
      "pagesize": yemianSize,
      "to_user": userId,
      "type": 0,
      "longitude": 0,
      "latitude": 0,
    };
    XinDongNetworking.instance
        .request(XinDongApi.momentUserList, method: XinDongHttpMethod.post, parameters: parameters)
        .then(callback);
  }

  Future<XinDongDongTaiClass?> requestDetails(dynamic id) async {
    var parameters = {"dy_id": id};
    final object = await XinDongNetworking.instance.request(
      XinDongApi.momentCommentList,
      method: XinDongHttpMethod.post,
      parameters: parameters,
    );

    if (object.code == XinDongServerStatus.success) {
      return XinDongDongTaiClass.fromJson(object.result);
    }
    return null;
  }

  // 请求打赏
  Future<bool> requestDaShang(XinDongDongTaiClass model) async {
    var parameters = {"dy_id": model.dy_id, "to_userid": model.uid};
    final object = await XinDongNetworking.instance.request(
      XinDongApi.momentDaShang,
      method: XinDongHttpMethod.post,
      parameters: parameters,
    );
    return object.code == XinDongServerStatus.success;
  }

  // 请求发起评论
  //
  // model 动态模型
  // content 评论内容
  Future<bool> requestComment(XinDongDongTaiClass model, {required String? content}) async {
    if (content == null || content.trim().isEmpty) {
      showToast("请输入内容");
      return false;
    }
    var parameters = {
      "dy_id": model.dy_id,
      "type": model.type,
      "touserid": model.userid,
      "content": content.trim(),
    };
    final object = await XinDongNetworking.instance.request(
      XinDongApi.momentComment,
      method: XinDongHttpMethod.post,
      parameters: parameters,
    );
    if (object.code == 201) {
      XinDongAlertMessage.show(
        content: object.msg,
        button: "立即开通",
        onTapButton: () {
          XinDongPageSingleton.instance.openNoble();
        },
      );
    }
    return object.code == XinDongServerStatus.success;
  }

  // 请求点赞
  Future<bool> requestPraise(XinDongDongTaiClass model, bool isthumbsup) async {
    var parameters = {"dy_id": model.dy_id, "type": isthumbsup ? 2 : 1};
    final object = await XinDongNetworking.instance.request(
      XinDongApi.momentThumbsUp,
      method: XinDongHttpMethod.post,
      parameters: parameters,
    );
    return object.code == XinDongServerStatus.success;
  }

  // 请求添加
  //
  // model 模型
  Future<bool> requestPublish({required XinDongDongTaiPublishClass model}) async {
    var parameters = model.toJson();
    parameters.addIf(true, "userid", XinDongUserController.to.loginId);
    final object = await XinDongNetworking.instance.request(
      XinDongApi.momentPublish,
      method: XinDongHttpMethod.post,
      parameters: parameters,
      addUserId: false,
    );
    return object.code == XinDongServerStatus.success;
  }

  // 请求删除动态
  Future<bool> requestDelete(XinDongDongTaiClass model) async {
    var parameters = {"id": model.dy_id};
    final object = await XinDongNetworking.instance.request(
      XinDongApi.momentDelete,
      method: XinDongHttpMethod.post,
      parameters: parameters,
    );
    return object.code == XinDongServerStatus.success;
  }
}
